package com.dic.mall.ware.service.impl;

import com.dic.common.utils.R;
import com.dic.mall.ware.feign.ProductFeignService;
import com.dic.mall.ware.vo.SkuHasStockVo;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dic.common.utils.PageUtils;
import com.dic.common.utils.Query;

import com.dic.mall.ware.dao.WareSkuDao;
import com.dic.mall.ware.entity.WareSkuEntity;
import com.dic.mall.ware.service.WareSkuService;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
        String skuId = (String)params.get("skuId");
        String wareId = (String)params.get("wareId");
        if(!Strings.isNullOrEmpty(skuId)){
            queryWrapper.eq("sku_id",skuId);
        }
        if(!Strings.isNullOrEmpty(wareId)){
            queryWrapper.eq("ware_id",wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId);
        List<WareSkuEntity> wareSkuEntities = this.baseMapper.selectList(queryWrapper);
        if(wareSkuEntities!=null || wareSkuEntities.size()==0){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            R info = productFeignService.info(skuId);
            try{
                //TODO 异常出现以后不会回滚
                Map skuInfo = (Map) info.get("skuInfo");
                wareSkuEntity.setSkuName((String)skuInfo.get("skuName"));
            }catch (Exception e){
                e.printStackTrace();
            }
            this.save(wareSkuEntity);
        }else{
            this.baseMapper.addStock(skuId,wareId,skuNum);
        }
    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {
        List<SkuHasStockVo> collect = skuIds.stream().map(skuId -> {
            SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
            //查询当前sku的总库存量
            Long count = this.baseMapper.getSkuStock(skuId);
            skuHasStockVo.setSkuId(skuId);
            if(count==null){
                skuHasStockVo.setHasStock(false);
            }else if(count>0){
                skuHasStockVo.setHasStock(true);
            }
            return skuHasStockVo;
        }).collect(Collectors.toList());
        return collect;
    }

}